/**
 * 
 */
package com.mwyq.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.mwyq.model.Lang;
import com.mwyq.model.News;
import com.mwyq.model.Page;
import com.mwyq.model.Topic;
import com.mwyq.service.EntityService;
import com.mwyq.service.NewsService;
import com.mwyq.service.TestService;
import com.mwyq.service.TopicService;
import com.mwyq.util.PageUtil;


@RestController
@RequestMapping("topic")
public class TopController {
	
	
	@Autowired
	private TopicService topicService;
	@Autowired
	private EntityService entityService;
	@Autowired
	private NewsService newsService;
	
	/*
	 * 获取所有的topic，无时间，无热度getAllTopics()
	 * 获取topic以时间排序getHotTopics()
	 * */
	public void allTop(ModelAndView view){
		List<Topic> topicAll = topicService.getHotTopics();
		List<HashMap<String, String>> topicAndNumList = new ArrayList<HashMap<String, String>>();
		
		Iterator<Topic> it = topicAll.iterator();
		while(it.hasNext()){
			HashMap<String, String> topicMap = new HashMap<String, String>();
			
			//得到话题的具体内容
			Topic topic = it.next();
			String topicid = topic.getTopic_id();
			String topiclabel = topic.getTopic_label();
			Date topictime = topic.getProducedtime();
			DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
			String topicproducedtime =fmt.format(topictime);
			topicMap.put("topiclabel", topiclabel);
			topicMap.put("topicid", topicid);
			topicMap.put("topicproducedtime", topicproducedtime);
			
//			System.out.println("topcontroller topicid ---------:"+topicid);
			
			//得到话题对应新闻的数量
			int newsNum = topicService.getNewsNumByTopicid(topicid);
			topicMap.put("newsNum", String.valueOf(newsNum));
			
			//得到话题对应新闻id
			int newsid = topicService.getNewsidByTopicid(topicid);
			topicMap.put("newsid", String.valueOf(newsid));
			
			//得到relation最大对应的新闻list
			List<News> newsOne = topicService.getTopicNews(topicid);
			if(newsOne!=null){
				News news = newsOne.get(0);
				String newsAuthor = news.getNews_author();
				String newsTitle = news.getNews_title();
				String newsContent = news.getNews_content();
				Date newsTime = news.getNews_time();
				topicMap.put("newsAuthor", String.valueOf(newsAuthor));
				topicMap.put("newsTitle", String.valueOf(newsTitle));
				topicMap.put("newsContent", String.valueOf(newsContent));
				topicMap.put("newsTime", String.valueOf(newsTime));
			}
			topicAndNumList.add(topicMap);//hashMap类型的
		}
		view.addObject("topicAndNumList",topicAndNumList);
		view.addObject("topicAll",topicAll);
	}
	/**
	 * 得到话题下的新闻数量
	 * */
	
	
	/*
	 *获取前10条hotTopic
	 * */
	public void dealTop(ModelAndView view) {
		List<Topic> hoTopics =  topicService.getHotTopics();
		view.addObject("hot", hoTopics);
	}
	private void dealEntity(ModelAndView view) {
		// TODO Auto-generated method stub
		Map<String, Integer> indexEntity = entityService.getIndexEntity();
		
		view.addObject("indexEntity", indexEntity);
	}
	@RequestMapping(value = "/",method=RequestMethod.GET)
	public ModelAndView index(HttpServletRequest request){
		
		/*
		 * 首页模块
		 * 1、top
		 * 2、图形显示
		 * 3、.....
		 * */
		ModelAndView view = new ModelAndView("/0711index");
		
		/**
		 * 得到所有话题，无热度，无时间排序
		 * */
		allTop(view);
		
		/**
		 * 1、top模块  dealTop 得到热贴
		 * */
		dealTop(view);
		
		
		
		/*
		 * 实体关系图
		 * */
		dealEntity(view);
		
		return view;
	}
	
	/**
	 * 获取话题的新闻并分页显示
	 * **/
	@RequestMapping(value = "/newsPage",params="page", method=RequestMethod.GET)
	public ModelAndView newsPage(HttpServletRequest request,@Param("page") String page){
		
		ModelAndView view = new ModelAndView("/WEB-INF/topic/topic_news");
		
		int total = topicService.getTopicNum();
		
		
		Page p = PageUtil.createPage(13, total, Integer.valueOf(page));
		
		List<Topic> topics = topicService.getTopicByPage(p);
		
		view.addObject("page", p);
		view.addObject("topics", topics);
		return view;
	}
	
	/**
	 * 获取单条热门话题详情
	 * */
	@RequestMapping(value = "/{id}/news", method=RequestMethod.GET)
	public ModelAndView listtopNews(HttpServletRequest request,@PathVariable("id") String id){
		
		System.out.println("topicid:"+id);
		
		ModelAndView view = new ModelAndView("/WEB-INF/topic/topic_news");
		
		List<News> topicAllNews = topicService.getTopicAllNews(id);
		
		List<News> topicNews = topicService.getTopicNews(id);
		
		System.out.println(topicNews);
		
		view.addObject("topicNews",topicNews);
		view.addObject("topicAllNews",topicAllNews);
		return view;
	}
	
	/**
	 * 获取话题新闻列表的单条新闻的新闻内容
	 * **/
	@RequestMapping(value = "/{id}/newsContent", method=RequestMethod.GET)
	public ModelAndView topicNewsContent(HttpServletRequest request,@PathVariable("id") int id){
		
		System.out.println("newsid-----:"+id);
		
		ModelAndView view = new ModelAndView("/WEB-INF/topic/topic_newsContent");
		
		List<News> topicNewsContent = newsService.getNewsContent(id);
		view.addObject("topicNewsContent",topicNewsContent);
		return view;
	}
	 
	
	
	/**
	 * 话题的分页显示
	 * */
	@RequestMapping(value = "/topicPage",params="page", method=RequestMethod.GET)
	public ModelAndView topicsPage(HttpServletRequest request,@Param("page") String page){
		
		ModelAndView view = new ModelAndView("/WEB-INF/topic/topics");
		
		int total = topicService.getTopicNum();
		
		
		Page p = PageUtil.createPage(13, total, Integer.valueOf(page));
		
		List<Topic> topics = topicService.getTopicByPage(p);
		
		view.addObject("page", p);
		view.addObject("topics", topics);
		return view;
	}
}
